package br.inf.solutions.apresentacoes.ssp.view.composer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.zkoss.zk.ui.Component;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.Textbox;

import br.com.vexillum.control.GenericControl;
import br.com.vexillum.model.CommonEntityActivated;
import br.com.vexillum.util.HibernateUtils;
import br.com.vexillum.util.ReflectionUtils;
import br.com.vexillum.util.SpringFactory;
import br.inf.solutions.apresentacoes.ssp.control.CityController;
import br.inf.solutions.apresentacoes.ssp.control.NeighborhoodController;
import br.inf.solutions.apresentacoes.ssp.control.StateController;
import br.inf.solutions.apresentacoes.ssp.model.City;
import br.inf.solutions.apresentacoes.ssp.model.Neighborhood;
import br.inf.solutions.apresentacoes.ssp.model.State;

@SuppressWarnings("serial")
public abstract class BaseAddressComposer<E extends CommonEntityActivated, G extends GenericControl<E>> extends BaseComposer<E, G> {

	protected State searchState;
	protected City searchCity;
	
	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
	}
	
	@SuppressWarnings("unchecked")
	public List<State> getStates(){
		StateController controlador = SpringFactory.getController("stateController", StateController.class, ReflectionUtils.prepareDataForPersistence(this));
		return (List<State>) controlador.doAction("listAll").getList();
	}
	
	@SuppressWarnings("unchecked")
	public List<City> getCitys(){
		if (searchState == null) {
			return new ArrayList<City>();
		} else {
			HashMap<String, Object> data = new HashMap<String, Object>();

			City cidade = new City();
			cidade.setState(searchState);

			data.put("entity", cidade);
			CityController controlador = SpringFactory.getController("cityController", CityController.class, data);
			List<City> list = (List<City>) controlador.doAction("searchByHQL").getList();
			HibernateUtils.initialize(list);
			return list;
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<Neighborhood> getNeighborhoods(){
		if (searchCity == null) {
			return new ArrayList<Neighborhood>();
		} else {
			HashMap<String, Object> data = new HashMap<String, Object>();

			Neighborhood bairro = new Neighborhood();
			bairro.setCity(searchCity);

			data.put("entity", bairro);
			NeighborhoodController controlador = SpringFactory.getController("neighborhoodController", NeighborhoodController.class, data);
			return (List<Neighborhood>) controlador.doAction("searchByCriteria").getList();
		}
	}
	
	protected void loadComboboxes() {
		Textbox tb;
		Combobox cb;

		tb = (Textbox) getComponentById(component, "fldStateHidden");
		cb = (Combobox) getComponentById(component, "fldState");

		if (tb != null) {
			cb.setValue(tb.getValue());
			setSearchState(new State(cb.getValue()));
		}

		tb = (Textbox) getComponentById(component, "fldCityHidden");
		cb = (Combobox) getComponentById(component, "fldCity");

		if (tb != null) {
			cb.setValue(tb.getValue());
			setSearchCity(new City(cb.getValue()));
		}
		
		tb = (Textbox) getComponentById(component, "fldNeighborhoodHidden");
		cb = (Combobox) getComponentById(component, "fldNeighborhood");

		if (tb != null) {
			cb.setValue(tb.getValue());			 
		}
	}
		
	public State getSearchState() {
		return searchState;
	}

	public void setSearchState(State searchState) {
		this.searchState = searchState;
	}

	public City getSearchCity() {
		return searchCity;
	}

	public void setSearchCity(City searchCity) {
		this.searchCity = searchCity;
	}
}